<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link href="/res/back/css/base.css" rel="stylesheet" type="text/css" />
<link href="/res/third/bootstrap3/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
<script src="/res/common/js/jquery.1.9.1.min.js" type="text/javascript"></script>
<script src="/res/third/artDialog/jquery.artDialog.js" type="text/javascript"></script>
<script src="/res/third/validate/jquery.validate.js" type="text/javascript"></script>
</head>
<body>
	<div class="panel panel-default panel-content" style="overflow: auto;">
		<div class="panel-heading" style="position: fixed;height: 190px;width: 99%;">
		<table  class="table table-hover table-bordered table-striped table-condensed" >
			<tr>
				<th >项目名称:</th>
				<td>${enquiry.enqProject.projectName!} </td>

				<th>发布公司:</th>
				<td>${(enquiry.companyName)!} </td>

				<th>所在地:</th>
				<td>${enquiry.enqProject.provinceName!}-${enquiry.enqProject.cityName!}</td>


				<th>
					质保要求:
				</th>
				<td>[#if enquiry.quality?? ]${enquiry.quality}年 [#else]${enquiry.guaranteeinfo!}[/#if]</td>


                <th>报价是否含运费</th>
                <td>[#if enquiry.shipPrice?? ][#if enquiry.shipPrice==1]含运费[#elseif enquiry.shipPrice==2]不含运费[/#if] [/#if]</td>
			</tr>
			<tr>
				<th>联系人:</th>
				<td>${enquiry.contacts!}</td>

				<th>联系电话:</th>
				<td>${enquiry.phone!}</td>

				<th>联系邮箱:</th>
				<td>${enquiry.email!}</td>

				<th>客户名称:</th>
				<td>${enquiry.enqProject.customerName!}</td>

                <th>报价是否含税</th>
                <td>${EnqEnquiryTaxEnum.getName(enquiry.taxPrice!3)}   </td>

			</tr>
			
			<tr>
                <th>发布日期:</th>
                <td>[#if enquiry.pubdate?? ]${enquiry.pubdate?string("yyyy-MM-dd")!''}[#else]无[/#if]</td>

				<th>截止日期:</th>
                <td>[#if enquiry.deadline?? ]${enquiry.deadline?string("yyyy-MM-dd")!''}[#else]无[/#if]</td>

				<th>创建时间:</th>
                <td>[#if enquiry.createtime?? ]${enquiry.createtime?string("yyyy-MM-dd HH:mm:ss")!''}[#else]无[/#if]</td>

				<th>更新时间:</th>
                <td>[#if enquiry.updatetime?? ]${enquiry.updatetime?string("yyyy-MM-dd HH:mm:ss")!''}[#else]无[/#if]</td>

                <th>
                    当前状态：
                </th>
                <td>
				${EnqEnquiryEnum.getName(enquiry.state)!}
                </td>

			</tr>

			<tr>
				<th>
					其他要求:
				</th>
				<td  >
					[#if enquiry.otherRemarks??]
						<a href="javascript:void(0)" onclick="art.dialog({title: '其他要求',width:500,content: '${enquiry.otherRemarks}'});" >查看其他要求</a>
					[#else]
					无
					[/#if]
				</td>
                <th>
                    附件:
                </th>
                <td >
					[#if enquiry.attachfile?? ]
						<a href="${enquiry.attachfile}" target="_blank" >${enquiry.attachfileName!"附件下载"}</a>
						[#else]
						无
					[/#if]
                </td>
				[#if enquiry.qtype==2]
                <th>
                    报价最高限价:
                </th>
                <td>
					${enquiry.fixedPrice!}元
				</td>
				[/#if]

			[#if enquiry.qtype==1 && enquiry.replaceProvideFiles?? && enquiry.replaceProvideFiles!="" ]
                <th>
                    需要:
                </th>
                <td>
				${enquiry.replaceProvideFiles!}
                </td>
			[/#if]

			</tr>

			<tr>
				<th>操作:</th>
				<td colspan="8">
				[#if enquiry.state == EnqEnquiryEnum.ENQ_STAY.index || enquiry.state == EnqEnquiryEnum.ENQ_RETRY.index  ]
					<button type="button" class="btn   btn-success" onclick="changeState(${enquiry.id},${EnqEnquiryEnum.ENQ_POSTED.index})" >通过审核</button>
					<button type="button" class="btn   btn-default" onclick="changeState(${enquiry.id},${EnqEnquiryEnum.ENQ_BACK.index})" >退回修改</button>
				[#elseif enquiry.state ==EnqEnquiryEnum.ENQ_POSTED.index ]	
					<button type="button" class="btn    btn-default" onclick="changeState(${enquiry.id},${EnqEnquiryEnum.ENQ_BACK.index})" >退回修改</button>
				[/#if ]	
				</td>


		[#if enquiryItems?? ]

			[#if enquiryItems?size != 0]
					<td>
						<button type="button" onclick="additem()" class="btn btn-info" >增加</button>
						<button type="button" onclick="submodel()" class="btn btn-info" >保存修改</button>
					</td>
			[#else]
            	<td></td>
			[/#if]
		[#else]
            <td></td>
		[/#if]
            </tr>
		</table>

	</div>
	
	

	<form id="itemform"  action="/enq/o_saveitem2.do" method="post"  style="padding-top: 190px;"  >
	<div >
		<input type="hidden" value="${enquiry.id}" name="enquiryid" id="enquiryid"  />
		<input type="hidden" value="true" name="onlysave" />
		
		<table  id="fenxi"  class="table table-hover table-bordered table-striped table-condensed">
			<thead>
				<tr>
					<th>#</th>
					<th>系统名称</th>
					[#list 1..6  as a]
					<th class="cell_${a}">
						<select class="typename" name="typename" attnum=${a} >
							[#--标题匹配--]
							<option  value="0"  >请选择</option>
							<option  [#if a==1] selected="selected" [/#if] value="1"  >设备名称</option>
							<option  [#if a==2] selected="selected" [/#if] value="2" >品牌</option>
							<option  [#if a==3] selected="selected" [/#if] value="3" >型号</option>
							<option  [#if a==4] selected="selected" [/#if] value="4" >单位</option>
							<option  [#if a==5] selected="selected" [/#if] value="5" >数量</option>
							<option  [#if a==6] selected="selected" [/#if] value="6" >技术参数</option>
						</select>
						
					</th>
					[/#list]
					<th>
						操作
					</th>
				</tr>
			</thead>
			[#if enquiryItems?? ]
			[#list enquiryItems  as enquiryitem ]
			<tr attindex="${enquiryitem_index}" >
				<td><span>${enquiryitem_index+1!}</span>
					<span>
						<input type="hidden"  name=".enquiryId" value="${enquiry.id!}"  />
					</span>
				</td>
				
				<td class="cell_0">
						<input id="system_${enquiryitem_index}" type="button"   value="${(enquiryitem.category.name)!"选择系统"}"  class="w120 btn [#if enquiryitem.category?? ]btn-success [#else]btn-danger[/#if] "   onclick="chooseService(this,${enquiryitem_index+1})"   />
						<input type="hidden" class="fcateid" name=".categoryPId"  value="${(enquiryitem.category.id)!}"  />
						<input type="hidden" name=".categorySId"  value="${enquiryitem.categoryS.id}"  />
						<input type="hidden" name=".needrepository"   value=0  />
				</td>
				
				<td class="cell_1" style="max-width: 150px;" >
				[#--<div style="max-height: 50px;overflow:auto;"> --]
				<input   name=""    class="input_1"     value="${enquiryitem.productName!}" >
				</td>
				<td class="cell_2"><input name=""    class="input_2" value="${enquiryitem.brand!}"></td>
				<td class="cell_3"><input name=""    class="input_3" value="${enquiryitem.model!}"></td>
				<td class="cell_4" ><input name=""    class="input_4" value="${enquiryitem.unit!}"></td>
				<td class="cell_5" ><input name=""    class="input_5" value="${enquiryitem.quantity!}"></td>
				<td class="cell_6" style="max-width: 150px;"><input name=""    class="input_6" value="${enquiryitem.descr!}"></td>
				<td>
					<button type="button" class="btn btn-xs btn-warning"  onclick="deletetr(this)" >删除</button>
				</td>
			</tr>
			[/#list]
			[/#if]
		</table>
		</div>
		</form>

	</div>
	<div style="display: none;">
		<tr>
			<td>
				
			</td>
		</tr>
	</div>
<style>
.aui_content{width:100%;height: 100%;padding: 0px;}	
.noselect{
	background-color: rgba(99, 230, 99, 0.8);
}
</style>

<script>
	$(".aui_content").removeAttr("style");
	//切换状态
	function changeState(enquiryid ,prostate){
		art.dialog.load("请做出选择","/enq/v_prostate_change.do",{enquiryid:enquiryid,prostate:prostate});
	}
	
	//切换状态后回调
	function changestatrecallback(){
		top.closeTempTab(true);
	}
	
	//添加行
	function additem(){
		
		var ctr = $("#fenxi").find("tr").last().clone(true);
		var indexnum = parseInt(ctr.attr("attindex"))+1;
		ctr.attr("attindex",indexnum);
		
		
		//序号	
		ctr.find("td").eq(0).find("span").eq(0).text(indexnum+1);
		var ctrhtml = ctr.html();
		
		//var abc = ctrhtml.replace(/item\[(\d+)\]/gm,"item["+indexnum+"]");
		//重置tr里的内容
		ctr.html(ctrhtml);
		
		ctr.find("input[class^='input_']").val("");
		
		$("#fenxi").append(ctr);
		
		$(".panel-content").scrollTop($(".panel-content")[0].scrollHeight);
		
	}
	//删除行
	function deletetr(obj){
		art.dialog.confirm("确认删除",function(){
		
		var _this = $(obj);
		var _parent = _this.parents("tr");
		var indexnum = parseInt(_parent.attr("attindex"));
		_parent.nextAll().each(function(){
			$(this).attr("attindex",indexnum);
			$(this).find("td").eq(0).find("span").eq(0).text(indexnum+1);
			//$(this).html($(this).html().replace(/item\[(\d+)\]/gm,"item["+indexnum+"]"));
			indexnum++;
		});
		_parent.remove();
		art.dialog.tips("已删除");	
		});
	}
	
	
	//设备名称，品牌 ，型号  ,单位 ,数量,技术参数
	var selectnames =["productName","brand","model","unit","quantity","descr"];
    //切换选项，确保不重复就可
	$(".typename").on("change",function(){
		//获取当前选值
		var  thisval = $(this).val();
		
		//将其他相同的选项设置为请选择
		if(thisval!=0){
			var hadvnum =[];
			hadvnum.push(thisval);
			
			$(".typename").each(function(i){
				var otherv = $(this).val();
				if(otherv == thisval ){
					$(this).val(0);
				}else{
					hadvnum.push(otherv);
				}
			});
			
			//没有被选择的加颜色
			var nums = hadvnum.join("");
			$(".noselect").removeClass("noselect");
			for(var i=1;i<7;i++){
				if(nums.indexOf(i)==-1){
					$(".typename").find("option[value='"+i+"']").addClass("noselect");
				}
			} 
			
		}
		
		$(this).val(thisval);
	})
	
	
	//切换系统开始
    var cobj;
    var cdialog;
    //选择器
    function chooseService(obj,index){
    	cobj=$(obj);
    	if(cdialog){
	    	cdialog.close();
    	}
    	var ccv = cobj.next().val();
    	cdialog=art.dialog.load("选择系统->序号:"+index,"/category/v_choose_category.do?curent="+ccv);
    }
    
    //选择回调
    function chooseCallback(name, id,topid){
    	if(name&&id){
	    	cobj.val(name);//显示名称
            cobj.removeClass("btn-danger").addClass("btn-success");
	    	cobj.next().val(id).next().val(topid).next().val(1);//显示值
    	}
    	cdialog.close();
    }
    //切换系统结束
	    
	    
	    
	    
	    
	    
	    
    
	//准备提交
	function submodel(){
		var success = true;
		var msg ="";
		$(".typename").each(function(){
			var thisval = $(this).val();
			if(thisval==0){
				success = false;
				msg="失败：请确定每一列的名称";
				return false;
			}
			
		});
		
		if(!success){
			art.dialog.tips(msg);
			return ;
		}else{
			//确定每列的name
			$(".typename").each(function(i){
				var thisval = $(this).val();
				var attnum = $(this).attr("attnum");
				//拿到name值
				var namev = selectnames[thisval-1];
				//设置name值
				$(".input_"+attnum).each(function(){
                    $(this).attr("name","."+namev);
				})
				
			});

            $(".fcateid").each(function(){
                var v = $(this).val();
                if(v=="" || v.indexOf(",")!=-1){
                    success=false;
                    return ;
                }
            })
            if(!success){
                art.dialog.tips("必须选择系统！");
                return ;
            }
			
			//验证数值 ,数量必须全为数字
			$("#itemform").find("input[name$='quantity']").each(function(){
				var num = $(this).val();
				if(num=="" || isNaN(num)||parseFloat(num)<0){
					success = false;
					return false;
				}
			});
			
			if(!success){
				art.dialog.tips("数量对应的列必须全为数字且能能小于0!");
				return ;
			}
			
			//验证设备名称
			$("#itemform").find("input[name$='productName']").each(function(){
				var productname = $(this).val();
				if(productname==""){
					success = false;
					return ;
				}
			});
			
			if(!success){
				art.dialog.tips("设备名称不能有空！");
				return ;
			}
			
			//单位不能为空
			$("#itemform").find("input[name$='unit']").each(function(){
				var unit = $(this).val();
				if(unit==""){
					success = false;
					return ;
				}
			});
			
			if(!success){
				art.dialog.tips("单位不能有空！");
				return ;
			}



            var items =[];
            var typenames =[];
            var oldtitles=[];
            $("#fenxi").find("tr").each(function () {
                var obj={};
                $(this).find("input[name]").each(function () {
                    var names = $(this).attr("name");
                    var inputval = $(this).val();
                    if(names.indexOf(".")!=-1){
                        var k = names.substring(1);
                        obj[k]=inputval;
                    }else{
                        if(names=="oldtitle"){
                            oldtitles.push(inputval);
                        }
                    }
                });
                if(!jQuery.isEmptyObject(obj)){
                    items.push(obj);
                }
            });

            $("select[name='typename']").each(function(){
                typenames.push($(this).val());
            });

            var obj={item:items,typenames:typenames,oldtitles:oldtitles,enquiryid:$("#enquiryid").val(),onlysave:true};

            //设置后提交
			art.dialog.post({
				url:"/enq/o_saveitem.do",
                data:JSON.stringify(obj),
                contentType:"application/json",
				success:function(data){
					if(data.success){
						art.dialog.alert("保存成功",function(){
							top.closeTempTab(true);
						})
					}else{
						art.dialog.tips(data.msg);
					}
				}
			});
			
		}
	}
</script>
</body>
</html>	